package com.augurit.common.syscode.convert;

import java.util.ArrayList;
import java.util.List;

import com.augurit.common.base.extmodel.BasicCombo;
import com.augurit.common.entity.SysCodeItem;
import com.augurit.common.entity.SysCodeType;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.syscode.web.form.SysCodeTypeForm;

public class SysCodeConverter {

	public static void convert(SysCodeForm form, SysCodeItem entity){
		if(form != null && entity != null){
			entity.setCode(form.getItemCode());
			entity.setTypeCode(form.getTypeCode());
			entity.setName(form.getItemName());
			entity.setStatus(form.getItemStatus());
			entity.setSortno(form.getItemSortno());
			entity.setMemo(form.getItemMemo());
		}
	}
	
	public static void convert(SysCodeForm form, SysCodeType entity){
		if(form != null && entity != null){
			entity.setCode(form.getTypeCode());
			entity.setName(form.getTypeName());
			entity.setGrade(form.getTypeGrade());
			entity.setMemo(form.getTypeMemo());
		}
	}
	
	public static SysCodeTypeForm convertToSysCodeTypeForm(SysCodeForm form){
		SysCodeTypeForm typeForm = null;
		if(form != null){
			typeForm = new SysCodeTypeForm();
			typeForm.setCode(form.getTypeCode());
			typeForm.setName(form.getTypeName());
			typeForm.setGrade(form.getTypeGrade());
			typeForm.setMemo(form.getTypeMemo());
			
		}
		return typeForm;
		
	}
	
	public static SysCodeForm convertToForm(SysCodeItem entity){
		if(entity != null){
			SysCodeForm form = new SysCodeForm();
			form.setItemId(entity.getId());
			form.setTypeCode(entity.getTypeCode());
			form.setItemCode(entity.getCode());
			form.setItemName(entity.getName());
			form.setItemStatus(entity.getStatus());
			form.setItemSortno(entity.getSortno());
			form.setItemMemo(entity.getMemo());
			return form;
		}else
			return null;
	}
	
	public static SysCodeForm convertToForm(SysCodeType entity){
		if(entity != null){
			SysCodeForm form = new SysCodeForm();
			form.setTypeId(entity.getId());
			form.setTypeCode(entity.getCode());
			form.setTypeName(entity.getName());
			form.setTypeGrade(entity.getGrade());
			form.setTypeMemo(entity.getMemo());
			return form;
		}else
			return null;
	}
	
	public static List<SysCodeForm> convertTypeToFormList(List<SysCodeType> list){
		if(list != null && list.size() > 0){
			List<SysCodeForm> result = new ArrayList();
			for(SysCodeType entity : list){
				SysCodeForm form = convertToForm(entity);
				result.add(form);
			}
			return result;
		}else
			return null;
	}
	
	public static List<SysCodeForm> convertItemToFormList(List<SysCodeItem> list){
		if(list != null && list.size() > 0){
			List<SysCodeForm> result = new ArrayList();
			for(SysCodeItem entity : list){
				SysCodeForm form = convertToForm(entity);
				result.add(form);
			}
			return result;
		}else
			return null;
	}
	
	public static List<BasicCombo> convertToBasicCombo(List<SysCodeForm> list, boolean allowBlank){
		List<BasicCombo> result = new ArrayList();
		
		if(allowBlank)
			result.add(BasicCombo.getBlankItem());
		
		if(list != null && list.size() > 0){
			for(SysCodeForm form : list){
				BasicCombo combo = new BasicCombo();
				combo.setLabel(form.getItemName());
				combo.setValue(form.getItemCode());
				result.add(combo);
			}
		}
		
		return result;
	}
}
